﻿using ESP.Tool.Excel;
using ShortUrl.Web.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Web;

namespace ShortUrl.Web.Models
{
    public class ShortUrlModel
    {
        /// <summary>
        /// 流水ID
        /// </summary>
        //public int ID { set; get; }

        /// <summary>
        /// 短地址
        /// </summary>
        public string ShortUrl { set; get; }

        /// <summary>
        /// 短地址Key
        /// </summary>
        public string ShortUrlKey { set; get; }

        /// <summary>
        /// 原Url地址
        /// </summary>
        public string SourceUrl { set; get; }

        public ShortUrlModel()
        {
            ShortUrl = "";
            SourceUrl = "";
            ShortUrlKey = "";
        }

        /// <summary>
        /// 创建时间
        /// </summary>
        //public DateTime CreateTime { set; get; }


        #region 方法
 
        /// <summary>
        /// 插入一条新ShortUrl记录
        /// </summary>
        /// <param name="SourceUrl"></param>
        /// <returns></returns>
        public static ShortUrlModel Create(string shortUrlDoMain,string SourceUrl)
        {
            
            ShortUrlModel m = new ShortUrlModel();
            m.SourceUrl = SourceUrl;

            try {
                int obj = 0;

                while (obj == 0)
                {
                    m.ShortUrlKey = Tool.RandomShortUrl.UniqueShortUrl();
                    m.ShortUrl = string.Format("{0}/{1}",shortUrlDoMain, m.ShortUrlKey);

                    string sql = @"INSERT INTO ShortUrlInfo (ShortUrl, SourceUrl,ShortUrlKey) VALUES (@url,@sUrl,@key);
SELECT LAST_INSERT_ROWID() FROM ShortUrlInfo";
                    //m.ShortUrl, m.SourceUrl,m.ShortUrlKey);
                    SQLiteParameter[] pArr = {
                        new SQLiteParameter("@url",m.ShortUrl),
                        new SQLiteParameter("@surl",m.SourceUrl),
                        new SQLiteParameter("@key",m.ShortUrlKey)
                    };

                    obj = Convert.ToInt32(SQLiteHelper.ExecuteScalar(sql,CommandType.Text, pArr));
                }
                
            }
            catch (Exception ex)
            {
                ESP.Tool.ErrLogFile.ErrLog(ex);
                m = null;
            }

            return m;
        }

        /// <summary>
        /// 获取所有短连接列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetList()
        {
            try
            {
                string sql = @"select * from shorturlinfo";
                DataTable dt = SQLiteHelper.ExecuteDataTable(sql);

                return dt;
            }
            catch (Exception ex)
            {
                ESP.Tool.ErrLogFile.ErrLog(ex);
                return null;
            }
        }


        public static string GetSourceUrl(string ShortUrlKey)
        {
            string SourceUrl = string.Empty;
            try
            {
                string sql = (@"select SourceUrl from shorturlinfo where ShortUrlKey=@key");
                SQLiteParameter[] param = { new SQLiteParameter("@key", DbType.String, ShortUrlKey) };

                SourceUrl = Convert.ToString(SQLiteHelper.ExecuteScalar(sql, CommandType.Text, param));
            }
            catch(Exception ex)
            {
                ESP.Tool.ErrLogFile.ErrLog(ex);
            }
             
            return SourceUrl;
        }


        #endregion

    }
}